<?php
class Sns
{
	const REST_VIDEO_SERVER="http://10.1.254.30:8005/rest/";
	const REST_MYTT_SERVER="http://10.1.254.30:8004/rest/";
	const FRIEND_URI="friend";
	const GROUP_URI="group";
	const EVENT_URI="event";
	const MYTT_URI="mytt";
	
	public static function isFriend($uid1, $uid2)
	{
		static $sa_sns_is_friend;
		if(isset($sa_friend_status[$uid1][$uid2]))
			return $sa_friend_status[$uid1][$uid2];
		else if(isset($sa_friend_status[$uid2][$uid1]))
			return $sa_friend_status[$uid2][$uid1];
		
		$ret = false;
		
		if ($uid1==0 || $uid2==0)
		{
			$ret = 0;
		}
		
		$url=self::REST_MYTT_SERVER . self::FRIEND_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->checkFriend($uid1, $uid2)->get();
			
			if ($result->isSuccess())
			{
				$ret = (int)$result->is_friend;
			}
			else
			{
				
				$ret = false;
			}
		}
		catch(Exception $e)
		{
			
		}
		return $ret;
	}
	
	public static function listGroup($uid, $perm)
	{
		$url=self::REST_MYTT_SERVER . self::GROUP_URI;
		$client = new Zend_Rest_Client($url);
		$data=array();
		try
		{
			$result=$client->getGroup($uid, $perm)->get();
			
			if ($result->isSuccess())
			{
				$data['status']=1;
				$groups=$result->groups;
				$data['groups']=array();
				foreach ((array)$groups as $key => $g)
				{
					$data['groups'][]=(object)$g;
				}
			}
			else
			{
				$data['status']=0;
				$data['groups']=array();
			}
		}
		catch(Exception $e)
		{
			
		}
		return $data;
	}
	
	public static function  listEvent($uid, $perm)
	{
		$url=self::REST_MYTT_SERVER . self::EVENT_URI;
		$client = new Zend_Rest_Client($url);
		$data=array();
		try
		{
			$result=$client->getEvent($uid, $perm)->get();
			
			if ($result->isSuccess())
			{
				$data['status']=1;
				$events=$result->events;
				$data['events']=array();
				foreach ((array)$events as $key => $e)
				{
					$data['events'][]=(object)$e;
				}
			}
			else
			{
				$data['status']=0;
				$data['events']=array();
			}
		}
		catch(Exception $e)
		{
			
		}
		return $data;		
	}
	
	public static function  checkGroup($uid, $gid, $perm)
	{
		$url=self::REST_MYTT_SERVER . self::GROUP_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->checkGroup($uid, $gid, $perm)->get();
			
			if ($result->isSuccess())
			{
				return $result->isvalid;
			}
			else
			{
				
				return false;
			}
		}
		catch(Exception $e)
		{
			
		}
		return false;
	}
	
	public static function  checkEvent($uid, $eid, $perm)
	{
		$url=self::REST_MYTT_SERVER . self::EVENT_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->checkEvent($uid, $eid, $perm)->get();
			
			if ($result->isSuccess())
			{
				return $result->isvalid;
			}
			else
			{
				return false;
			}
		}
		catch(Exception $e)
		{
			
		}
		return false;
	}
	
	public static function  inviteGroup($uid, $gid, $invited_uid)
	{
		if ($uid==0 || $gid==0 || $invited_uid==0)
		{
			return 0;
		}
		$url=self::REST_MYTT_SERVER . self::GROUP_URI;
		try
		{
			$client = new Zend_Rest_Client($url);
			$result=$client->inviteGroup($uid, $gid, $invited_uid)->get();
			
			if ($result->isSuccess())
			{
				return $result->is_invited;
			}
			else
			{
				return 0;
			}
		}
		catch(Exception $e)
		{
			
		}
		return 0;
	}
	
	public static function  inviteEvent($uid, $eid, $invited_uid)
	{
		if ($uid==0 || $eid==0 || $invited_uid==0)
		{
			return 0;
		}
		$url=self::REST_MYTT_SERVER . self::EVENT_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->inviteEvent($uid, $eid, $invited_uid)->post();
			
			if ($result->isSuccess())
			{
				return $result->is_invited;
			}
			else
			{
				return 0;
			}
		}
		catch(Exception $e)
		{
			
		}
		return 0;
	}
	
	public static function getThank($uid, $sendId=0, $nid=0, $module=0)
	{
		$url=self::REST_MYTT_SERVER . self::MYTT_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->getThank($uid, $sendId, $nid, $module)->get();

			if ($result->isSuccess())
			{
				return array("uid" => $uid, "username" => $result->username, "current_mark" => $result->current_mark, "thank_counting" => $result->thank_counting);
			}
			else
			{
				return array();
			}
		}
		catch(Exception $e)
		{
			
		}
		return array();		
	}
	
	public static function setThank($uid, $val, $sent_uid, $nid, $module)
	{
		$url=self::REST_MYTT_SERVER . self::MYTT_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->setThank($uid, $val, $sent_uid, $nid, $module)->get();
			
			if ($result->isSuccess())
			{
				return array("uid" => $uid, "username" => $result->username, "current_mark" => $result->current_mark, "thank_counting" => $result->thank_counting);
			}
			else
			{
				return array();
			}
		}
		catch(Exception $e)
		{
			
		}
		return array();		
	}
	
	public static function addFeed($c_user, $type, $user, $created, $desc, $bookmark, $nid, $status, $privacy, $o_user)
	{
		$url=self::REST_MYTT_SERVER . self::MYTT_URI;
		$client = new Zend_Rest_Client($url);
		try
		{
			$result=$client->addFeed($c_user, $type, $user, $created, $desc, $bookmark, $nid, $status, $privacy, $o_user)->post();
			
			if ($result->isSuccess())
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		catch(Exception $e)
		{
			
		}
		return 0;			
	}
}

